//
//  File.swift
//  
//
//  Created by 钟志远 on 2021/3/26.
//

import Foundation
import Vapor

typealias CustomDataFuture<T: Content> = EventLoopFuture<CustomResponseData<T>>

extension Content {
    func successResponse(message: String = "ok") -> CustomResponseData<Self> {
        return CustomResponseData.success(message: message, data: self)
    }
}

extension EventLoopFuture where Value: Content {
    func format(msg: String = "ok") -> CustomDataFuture<Value> {
        map{$0.successResponse()}
    }
}

func makeSuccessData() -> CustomResponseData<[String: String]> {
    return [:].successResponse()
    
}

struct CustomResponseData<T: Content>: Content {
    var code: Int
    var message: String
    var data: T

    static func success(message: String, data: T) -> CustomResponseData {
        return CustomResponseData(code: 0, message: message, data: data)
    }
    
    static func fail(code:Int, message: String, data: T) -> CustomResponseData {
        return CustomResponseData(code: 0, message: message, data: data)
    }
    
}

